package com.open.party.cloud.view.home.article

import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentPagerAdapter
import cn.android.x.model.SuperItemBean
import cn.android.x.parent.AppTitleBaseActivity
import com.open.party.cloud.R
import com.open.party.cloud.view.home.article.fragment.ArticleListFragment
import com.sinothk.android.utils.XUtils
import kotlinx.android.synthetic.main.title_tab_view_page.*

class ArticleMainActivity : AppTitleBaseActivity() {

//    private var titleList: ArrayList<String>? = null
//    private var typeList: ArrayList<String>? = null

    private var itemList: ArrayList<SuperItemBean>? = null

    private var title = ""
    private var index = 0

    override fun getLayoutResId(): Int = R.layout.title_tab_view_page

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        itemList = intent.getSerializableExtra("itemList") as ArrayList<SuperItemBean>
        title = intent.getStringExtra("title") as String
        if (itemList!!.size == 1) {
            title =itemList!![0].text
        }

        index = intent.getIntExtra("index", 0)
        setViewTitle(XUtils.string().getNotNullValue(title))

        initTabView(itemList!!)
    }

    private fun initTabView(itemList: ArrayList<SuperItemBean>) {
        val titleList = ArrayList<String>()
        val typeList = ArrayList<String>()

        for (item in itemList) {
            titleList.add(item.text)
            typeList.add(item.type)
        }

        initTab(
            titleList,
            getFragments(typeList)
        )
    }

    private fun getFragments(typeList: java.util.ArrayList<String>): List<Fragment> {
        val fragments: ArrayList<Fragment> = ArrayList()

        for (type in typeList) {
            val fragment = ArticleListFragment()
            val bundle = Bundle()
            bundle.putString("type", type)
            fragment.arguments = bundle
            fragments.add(fragment)

            Log.e("type", type)
        }

        return fragments
    }

    private fun initTab(titles: ArrayList<String>, fragments: List<Fragment>) {

        val fragmentPagerAdapter = object : FragmentPagerAdapter(supportFragmentManager, FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {
            override fun getCount(): Int {
                return fragments.size
            }

            override fun getItem(arg0: Int): Fragment {
                return fragments[arg0]
            }
        }

        if (itemList!!.size == 1) {
            scrollTabView.visibility = View.GONE
        } else {
            scrollTabView.visibility = View.VISIBLE
        }

        viewPager.offscreenPageLimit = titles.size - 1
        viewPager.adapter = fragmentPagerAdapter
        //        pager.addOnPageChangeListener(this);

        scrollTab.setTitles(titles, index, titles.size <= 4)
//        scrollTab.setNumber(1, "9", View.VISIBLE)//设置数字红点
        scrollTab.setViewPager(viewPager)
        scrollTab.setOnTabListener { index, _ ->
            viewPager.setCurrentItem(index, true)
        }
    }
}